<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/13
 * Time: 17:15
 */

namespace App\Http\Controllers\Web;


use App\Service\PermissionService;

class PermissionController extends BaseController
{
    public function index()
    {
        $params = $this->request->post();
        return PermissionService::getList($params,'order','ASC');
    }

    public function add()
    {
        $params = $this->request->post();
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'权限名称不能为空']);
        }
        if(!isset($params['slug']) || empty(trim($params['slug']))){
            return Response()->json(['code'=>201,'msg'=>'权限标识不能为空']);
        }
        $params['parent_id'] = $this->request->post('parent_id',0);
        $params['order'] = $this->request->post('order',0);

        return PermissionService::add($params);

    }

    public function getPermissionById()
    {
        $permissionId = $this->request->post('id');
        if(!$permissionId){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        return PermissionService::getPermissionById($permissionId);

    }

    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        if(!isset($params['name']) || empty(trim($params['name']))){
            return Response()->json(['code'=>201,'msg'=>'权限名称不能为空']);
        }

        if(!isset($params['slug']) || empty(trim($params['slug']))){
            return Response()->json(['code'=>201,'msg'=>'权限标识不能为空']);
        }
        $params['parent_id'] = $this->request->post('parent_id',0);
        $params['order'] = $this->request->post('order',0);
        return PermissionService::edit($params);

    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的权限']);
        }
        return PermissionService::destroy($ids);

    }

}
